package com.ejia.server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.ejia.dao.BaseDao;
import com.ejia.pojo.*;

public class AdminService {

	public static ArrayList<notice> notices(String nid) {
		String sql="select * from tb_notice where nid like ?";
		ArrayList<notice> notices=new ArrayList<>();
		notice notice=null;
		BaseDao baseDao= new BaseDao();
		ResultSet rSet=baseDao.query(sql,"%"+nid+"%");
		try {
			while (rSet.next()) {
				notice = new notice();
				notice.setTitle(rSet.getString("title"));
				notice.setNid(rSet.getInt("nid"));
				notice.setContent(rSet.getString("content"));
				notice.setNoticetype(rSet.getString("noticetype"));
				notices.add(notice);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return notices;
	}
	
	public static String notices(int nid) {
		String sql="select * from tb_notice where nid=?";
		BaseDao baseDao= new BaseDao();
		ResultSet rSet=baseDao.query(sql,nid);
		try {
			while (rSet.next()) {
				return rSet.getString("content");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	
	public static int nid(String name) {
		String sql="select nid from tb_notice where title=?";
		BaseDao baseDao= new BaseDao();
		ResultSet rSet=baseDao.query(sql,name);
		try {
			while (rSet.next()) {
				return rSet.getInt("nid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	
	
	public static ArrayList<Admin> find(String aid) {
		String sql="select * from tb_admin where adminname like ?";
		ArrayList<Admin> admins=new ArrayList<>();
		Admin admin =null;
		BaseDao baseDao= new BaseDao();
		ResultSet rSet=baseDao.query(sql,"%"+aid+"%");
		try {
			while (rSet.next()) {
				admin = new Admin();
				admin.setId(rSet.getInt("id"));
				admin.setAdminname(rSet.getString("adminname"));
				admin.setAdminpwd(rSet.getString("adminpwd"));
				admin.setRemark(rSet.getString("remark"));
				admins.add(admin);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admins;
	}
	
	public static boolean add(String name,String pwd,String zw) {
		String sql="insert into tb_admin(adminname,adminpwd,remark) values(?,?,?)";
		return new BaseDao().execute(sql, name,pwd,zw);
	}
	
	public static boolean delete(int id) {
		String sql="delete from tb_admin where id=?";
		return new BaseDao().execute(sql, id);
	}
	
	
	public static boolean update(int id,String name,String pwd,String zw) {
		String sql="update tb_admin set adminname=?,adminpwd=?,remark=? where id=?";
		return new BaseDao().execute(sql, name,pwd,zw,id);
	}
	
	
	
}
